package com.example.farmer.Adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.farmer.R;

import java.util.ArrayList;
import java.util.List;

public class LeftListAdapter extends RecyclerView.Adapter<LeftListAdapter.Myholder> {

    private List<String> dataList =new ArrayList<>();

    private int currentIndex=0;

    public LeftListAdapter(List<String> dataList) {
        this.dataList = dataList;
    }

    @NonNull
    @Override

    public Myholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.left_list_item,null);
        return new Myholder(view);
    }

    @Override
    @SuppressLint("RecyclerView")
    public void onBindViewHolder(@NonNull Myholder holder,  int position) {
        //绑定数据
        String name = dataList.get(position);
        holder.tv_name.setText(name);

        // 分类的点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null!=mleftListOnClickItemListener){
                    mleftListOnClickItemListener.onItemClick(position);
                }
            }
        });

        if (currentIndex ==position){
            holder.itemView.setBackgroundResource(R.drawable.type_selector_bg);
        }else {
            holder.itemView.setBackgroundResource(R.drawable.type_selector_normal_bg);
        }



    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    static class Myholder extends RecyclerView.ViewHolder{
        TextView tv_name;
        public Myholder(@NonNull View itemView) {
            super(itemView);
            tv_name =itemView.findViewById(R.id.name);
        }
    }


    private LeftListOnClickItemListener mleftListOnClickItemListener;

    public void setMleftListOnClickItemListener(LeftListOnClickItemListener mleftListOnClickItemListener) {
        this.mleftListOnClickItemListener = mleftListOnClickItemListener;
    }

    public  interface LeftListOnClickItemListener{
        void  onItemClick(int position);
    }

    //方法
    public void setCurrentIndex(int position){
        this.currentIndex =position;
        notifyDataSetChanged();
    }
}
